<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Kuesioner extends CI_Controller {

   function __construct(){
   parent::__construct();
   $this->load->model('kuesioner_model');
   $this->load->model('uji_model');
   }
   
   function index()
	{
		if($this->session->userdata('username') == null)
		{
			redirect('guest');
		} else
			{
				$this->load->view('input_kuesioner_2');
			}
	}
	
	function tambah_kuesioner($param){
	$this->load->model('kuesioner_model');
	$pert_kues = $this->input->post('kues');
	$jns_kues = $this->input->post('jns_kues');
	$jns_ident = $this->input->post('jenis_ident');
	$a='';
	if($param == 'simpan'){
	$file = $_FILES['file']['tmp_name'][0];
		if(!empty($file)) 
		{
			if($uploaded = $this->do_upload('file')) 
			{
				//var_dump($uploaded);
				//$this->lokasi_model->insert_file($id_progres, $uploaded['raw_name'], $uploaded['orig_name'], $uploaded['file_ext'], date('Y-m-d'));								
				$id_kues = $this->kuesioner_model->insert_kuesioner($this->input->post('nama'), $this->session->userdata('id_user'), $this->input->post('deskripsi'), $uploaded['file_name'], md5($this->input->post('nama')), $this->input->post('pp'), $this->input->post('pm'), $this->input->post('pd'));
				//var_dump($uploaded);
			} else 
				{
					echo '<script>alert("file harus format pdf, doc, atau docx!");</script>';
				}
		}
		
		for($i=0; $i < count($pert_kues); $i++)
		{
			//var_dump($pert_kues);
			if($jns_kues[$i] == 1)
			{
				$this->kuesioner_model->insert_pertanyaan_teks($pert_kues[$i], $id_kues, 1, $jns_ident[$i]);
			} else 
				{
					if($jns_kues[$i] == 3)
					{
						if($a==''){
							foreach($this->input->post('skala') as $skala)
							{
								$a .= $skala.',';
							}
								//var_dump($a);
							$id_skala = $this->kuesioner_model->insert_skala($a);
							$this->kuesioner_model->insert_pertanyaan_skala($pert_kues[$i], $id_kues, 3, $id_skala);
						} else
							{
								$this->kuesioner_model->insert_pertanyaan_skala($pert_kues[$i], $id_kues, 3, $id_skala);
							}
					} else
						{
							if($jns_kues[$i] == 2)
								{
									if($b==''){
										foreach($this->input->post('pg') as $pg)
										{
											$b .= $pg.',';
										}
											//var_dump($a);
										$id_skala_pg = $this->kuesioner_model->insert_pilihan($b);
										$this->kuesioner_model->insert_pertanyaan_pilihan($pert_kues[$i], $id_kues, 2, $id_skala_pg);
									} else
										{
											$this->kuesioner_model->insert_pertanyaan_pilihan($pert_kues[$i], $id_kues, 2, $id_skala_pg);
										}
								}
						}
						
					
				}
			
		}
		$alerts[] = array('success', 'Kuesioner berhasil ditambahkan!');
		$this->session->set_flashdata('alerts', $alerts);
		redirect('kuesioner/list_kuesioner');
	} else
		{
			var_dump($this->input->post('nama'));
			
			
		}

	}
	
	function list_kuesioner()
	{
		$this->load->model('kuesioner_model');
		$id_user = $this->session->userdata('id_user');
		if($id_user)
		{
			$data['kuesioner'] = $this->kuesioner_model->get_kuesioner_by_iduser($id_user);
			//$var_dump($data);
			
			$this->load->view('home',$data);
		} else{
			redirect('guest');
		}
	}
	
	function list_data()
	{
		$this->load->model('kuesioner_model');
		$id_user = $this->session->userdata('id_user');
		if($id_user)
		{
			$data['kuesioner'] = $this->kuesioner_model->get_data_by_iduser($id_user);
			//$var_dump($data);
			$this->load->view('list_data',$data);
		} else{
			redirect('guest');
		}
	}
	
	function share_link($id_kuesioner)
	{
		$this->load->model('kuesioner_model');
		$data['kuesioner'] = $this->kuesioner_model->get_kuesioner($id_kuesioner);
		$this->load->view('modal_share_link', $data);
		//var_dump($data);
	}
	
	function form_isi_kuesioner($link)
	{
		$exp = explode('?',$link);
		$id_kuesioner = $exp[0];
		$this->load->model('kuesioner_model');
		$data['kuesioner'] = $this->kuesioner_model->get_kuesioner($id_kuesioner);
		$data['konten_teks'] = $this->kuesioner_model->get_konten_teks($id_kuesioner);
		$data['konten_skala'] = $this->kuesioner_model->get_konten_skala($id_kuesioner);
		$data['konten_pg'] = $this->kuesioner_model->get_konten_pg($id_kuesioner);
		//var_dump($data);
		//explode(',', $variable);
		$this->load->view('isi_kuesioner', $data);
	}
	
	function edit_kuesioner($id_kuesioner)
	{
		$this->load->model('kuesioner_model');
		$data['kuesioner'] = $this->kuesioner_model->get_kuesioner($id_kuesioner);
		$data['konten_teks'] = $this->kuesioner_model->get_kuesioner_teks($id_kuesioner);
		$data['konten_skala'] = $this->kuesioner_model->get_kuesioner_skala($id_kuesioner);
		$data['konten_pg'] = $this->kuesioner_model->get_kuesioner_pg($id_kuesioner);
		$data['skala'][] = $this->kuesioner_model->get_skala($id_kuesioner);
		$this->load->view('edit_kuesioner', $data);
		//var_dump($data['skala']);
	}
	
	function update_kuesioner()
	{
		
		$pert_kues = $this->input->post('kues');
		$jns_kues = $this->input->post('jns_kues');
		$id_kuesioner = $this->input->post('id_kues');
		$a='';
		$pg_lama_lagi='';
		
		$pert_lama = $this->input->post('pertanyaan');
		$jns_kues_lama = $this->input->post('jns_kues_lama');
		$id_pert_lama = $this->input->post('id_pertanyaan');
		$this->kuesioner_model->update_kuesioner($this->input->post('nama'), $this->input->post('deskripsi'), $id_kuesioner);
		
		$id_pertanyaan = $this->kuesioner_model->get_pertanyaan($id_kuesioner);
		$id_responden = $this->kuesioner_model->get_responden_kuesioner_byidpertanyaan($id_pertanyaan[0]['id_pertanyaan']);
		foreach($id_responden as $id_respond)
		{
			$this->uji_model->delete_responden($id_respond['fk_id_responden']);
		}
		
		
		if(!empty($_FILES['file']['tmp_name'][0])) 
		{
			if($uploaded = $this->do_upload('file')) 
			{
				//var_dump($uploaded);
				//$this->lokasi_model->insert_file($id_progres, $uploaded['raw_name'], $uploaded['orig_name'], $uploaded['file_ext'], date('Y-m-d'));								
				$this->kuesioner_model->update_file($uploaded['file_name'], $id_kuesioner);
				//var_dump($uploaded);
			} else 
				{
					echo '<script>alert("file harus format pdf, doc, atau docx!");</script>';
				}
		}
		
		if($this->input->post('skala_baru'))
		{
			foreach($this->input->post('skala_baru') as $skala)
			{
			$a .= $skala.',';
			}
			$id_skala = $this->kuesioner_model->insert_skala($a);
		} else
			{
				foreach($this->input->post('skala') as $skala)
				{
				$a .= $skala.',';
				}
				$this->kuesioner_model->update_skala($a, $id_skala = $this->input->post('id_skala'));
			}
			
		if($id_pert_lama)
		{
			for($i=0; $i < count($pert_lama); $i++)
			{
				if($this->input->post('skala_baru'))
				{
					if($jns_kues_lama[$i] == 3)
					{
						$this->kuesioner_model->update_kuesskala_lama($pert_lama[$i], $id_pert_lama[$i], $id_skala);
					} else
						{
							$this->kuesioner_model->update_pertanyaan_lama($pert_lama[$i], $id_pert_lama[$i]);
						}
				} else {
						$this->kuesioner_model->update_pertanyaan_lama($pert_lama[$i], $id_pert_lama[$i]);
					}
				
				if($jns_kues_lama[$i] == 2)
				{
					$pg_lama = $this->input->post('pg_lama_'.$id_pert_lama[$i]);
					foreach($pg_lama as $pg_lama)
					{
						$pg_lama_lagi .= $pg_lama.',';
					}
					//var_dump($pg_lama_lagi);
					$this->kuesioner_model->update_pg($pg_lama_lagi, $this->input->post('id_pilihan_'.$id_pert_lama[$i]));
					$this->kuesioner_model->update_pertanyaan_lama($pert_lama[$i], $id_pert_lama[$i]);
				}
			}
		} 
		
		if($pert_kues)
		{
			for($i=0; $i < count($pert_kues); $i++)
			{
				//var_dump($pert_kues);
				if($jns_kues[$i] == 1)
				{
					$this->kuesioner_model->insert_pertanyaan_teks($pert_kues[$i], $id_kuesioner, 1);
				} else 
					{
						if($jns_kues[$i] == 3)
						{					
							$this->kuesioner_model->insert_pertanyaan_skala($pert_kues[$i], $id_kuesioner, 3, $id_skala);
						} else
							{
								if($jns_kues[$i] == 2)
								{
									if($b==''){
										foreach($this->input->post('pg') as $pg)
										{
											$b .= $pg.',';
										}
											//var_dump($a);
										$id_skala_pg = $this->kuesioner_model->insert_pilihan($b);
										$this->kuesioner_model->insert_pertanyaan_pilihan($pert_kues[$i], $id_kuesioner, 2, $id_skala_pg);
									} else
										{
											$this->kuesioner_model->insert_pertanyaan_pilihan($pert_kues[$i], $id_kuesioner, 2, $id_skala_pg);
										}
								}
							}
					}
				
			}
		}
		
		$alerts[] = array('message', 'Kuesioner berhasil diupdate!');
		$this->session->set_flashdata('alerts', $alerts);
		redirect('kuesioner/list_kuesioner');
	}
	
	function delete_pertanyaan()
	{
		$this->load->model('kuesioner_model');
		$this->kuesioner_model->delete_pertanyaan($_POST['id_pertanyaan']);
		if($_POST['id_pg'])
		{
			$this->kuesioner_model->delete_pg($_POST['id_pg']);
		}
	}
	
	function delete_skala_lama()
	{
		$this->load->model('kuesioner_model');
		$this->kuesioner_model->delete_skala($_POST['id_skala']);
	}
	
	function do_upload($field)
	{
		$this->load->helper('file');
		
		$dir = 'file/proposal/';
		if(!is_dir($dir)) {
			mkdir($dir, 0777, true);
		}
				
		$config['upload_path'] = $dir;
		$config['encrypt_name'] = true;
		$config['remove_spaces'] = true;
		$config['allowed_types'] = 'pdf';
		
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if($this->upload->do_upload($field)) {
			$list_uploaded = $this->upload->data();			
			return $list_uploaded;
		}
		else{
		}
	}
	
	function isi_kuesioner()
	{
		$pertanyaan = $this->input->post('id_pertanyaan');
		$jawaban = $this->input->post('jawab');
		//untukjmeter -> $pertanyaan[0] = $this->input->post('id_pertanyaan');
		//untukjmeter -> $jawaban[0] = $this->input->post('jawab');
		//var_dump($pertanyaan);
		//var_dump($jawaban);
		$this->load->model('kuesioner_model');
		$id_responden = $this->kuesioner_model->insert_responden('responden1');
		for($i = 0; $i < count($pertanyaan); $i++)
		{
		$id_jawaban = $this->kuesioner_model->insert_jawaban_responden($jawaban[$i]);
		$this->kuesioner_model->insert_responden_kuesioner($id_responden, $pertanyaan[$i], $id_jawaban);
		}
		redirect('kuesioner/list_kuesioner');
		
		//var_dump($this->input->post());
	}
	
	function lihat_data_responden($id_kuesioner)
	{
		$this->load->model('kuesioner_model');
		$data_responden = array();
		$data['pertanyaan'] = $this->kuesioner_model->get_pertanyaan($id_kuesioner);
		$data['responden'] = $this->kuesioner_model->responden($id_kuesioner);
		$data['kuesioner'] = $this->kuesioner_model->get_kuesioner($id_kuesioner);
		foreach($data['responden'] as $responden)
		{
			$pertanyaan['data_jawaban'] = $this->kuesioner_model->get_jawaban($responden['fk_id_responden']);
			$data_responden[] = $pertanyaan;
		}
		$data['data_responden'] = $data_responden;
		//var_dump($data);
		$this->load->view('modal_preview_data', $data);
	}
	
	function lihat_proposal($file)
	{
		$data['file'] = $file;
		//var_dump($data);
		$this->load->view('modal_preview_proposal', $data);
	}
	
	function hapus_penelitian($id_kuesioner)
	{
		$id_pertanyaan = $this->kuesioner_model->get_id_pertanyaan($id_kuesioner);
		$id_responden = $this->kuesioner_model->get_fk_id_responden($id_pertanyaan[0]['id_pertanyaan']);
		//var_dump($id_responden);
		foreach($id_responden as $responden)
		{
			$data = $this->uji_model->get_id_jawaban_responden($responden['fk_id_responden']);
			foreach($data as $jawaban)
			{
				//var_dump($jawaban['fk_id_jawaban_responden']);
				$this->uji_model->delete_jawaban_responden($jawaban['fk_id_jawaban_responden']);
			}
			$this->uji_model->delete_responden($responden['fk_id_responden']);
		}
		
		foreach($id_pertanyaan as $pertanyaan)
		{
			$this->kuesioner_model->delete_pertanyaan($pertanyaan['id_pertanyaan']);
			if($pertanyaan['fk_id_jenis_pertanyaan'] == 2)
			{
				$this->kuesioner_model->delete_pilihan($pertanyaan['fk_id_pilihan']);
			}
			if($pertanyaan['fk_id_jenis_pertanyaan'] == 2)
			{
			$id_skala = $pertanyaan['fk_id_skala'];
			}
		}
		
		if(!empty($id_skala)){
			$this->kuesioner_model->delete_skala($id_skala);
		}
		
		$this->kuesioner_model->delete_kuesioner($id_kuesioner);
		redirect('kuesioner/list_kuesioner');
	}
	
	function cetak_kuesioner($id_kuesioner)
	{
		$this->load->model('kuesioner_model');
		$data['kuesioner'] = $this->kuesioner_model->get_kuesioner($id_kuesioner);
		$data['konten_teks'] = $this->kuesioner_model->get_konten_teks($id_kuesioner);
		$data['konten_skala'] = $this->kuesioner_model->get_konten_skala($id_kuesioner);
		$data['konten_pg'] = $this->kuesioner_model->get_konten_pg($id_kuesioner);
		//var_dump($data);
		//explode(',', $variable);
		$this->load->view('print_kuesioner', $data);
	}
	
	
	
}
?>
